package handler

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop_srvs/goods_srv/global"
	"mxshop_srvs/goods_srv/model"
	"mxshop_srvs/goods_srv/proto"
)

// 通过category获取brands

/*
*
分页查询CategoryBrand
*/
func (g *GoodsServer) CategoryBrandList(ctx context.Context, req *proto.CategoryBrandFilterRequest) (*proto.CategoryBrandListResponse, error) {
	categoryBrandList := proto.CategoryBrandListResponse{}
	var categoryBrands []*model.GoodsCategoryBrand
	var total int64
	var categoryBrandResponse []*proto.CategoryBrandResponse
	//获取总的数量
	global.DB.Model(&model.GoodsCategoryBrand{}).Count(&total)
	categoryBrandList.Total = int32(total)
	//分页查询分类和数量的关系
	global.DB.Preload("Category").Preload("Brands").Scopes(Paginate(int(req.Pages), int(req.PagePerNums))).Find(&categoryBrands)
	for _, categoryBrand := range categoryBrands {
		categoryBrandResponse = append(categoryBrandResponse, &proto.CategoryBrandResponse{
			Brand: &proto.BrandInfoResponse{
				Id:   categoryBrand.Brands.ID,
				Name: categoryBrand.Brands.Name,
				Logo: categoryBrand.Brands.Name,
			},
			Category: &proto.CategoryInfoResponse{
				Id:             categoryBrand.Category.ID,
				Name:           categoryBrand.Category.Name,
				ParentCategory: categoryBrand.Category.ParentCategoryId,
				Level:          categoryBrand.Category.Level,
				IsTab:          categoryBrand.Category.IsTab,
			},
		})
	}
	categoryBrandList.Data = categoryBrandResponse
	return &categoryBrandList, nil
}

/*
*
根据分类信息，查询分类下所有的品牌
*/
func (g *GoodsServer) GetCategoryBrandList(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.BrandListResponse, error) {
	var brandListResponse proto.BrandListResponse
	var brandInfoResponses []*proto.BrandInfoResponse
	var total int64

	//查询分类是否存在
	var category model.Category
	if result := global.DB.First(&category, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "找不到当前分类")
	}
	brandListResponse.Total = int32(total)
	var categoryBrandsList []model.GoodsCategoryBrand
	global.DB.Preload("Brands").Where(&model.GoodsCategoryBrand{CategoryId: req.Id}).Find(&categoryBrandsList)
	if len(categoryBrandsList) > 0 {
		for _, categoryBrand := range categoryBrandsList {
			brandInfoResponses = append(brandInfoResponses, &proto.BrandInfoResponse{
				Id:   categoryBrand.Brands.ID,
				Name: categoryBrand.Brands.Name,
				Logo: categoryBrand.Brands.Logo,
			})
		}
	}
	brandListResponse.Data = brandInfoResponses
	return &brandListResponse, nil
}

/*
*
创建分类品牌的关联
*/
func (g *GoodsServer) CreateCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*proto.CategoryBrandResponse, error) {
	//判断分类是否存在
	var category model.Category
	var brand model.Brands
	if categoryResult := global.DB.First(&category, req.CategoryId); categoryResult.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "找不到当前分类")
	}

	if brandResult := global.DB.First(&brand, req.BrandId); brandResult.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "找不到当前品牌")
	}

	categoryBrand := model.GoodsCategoryBrand{
		CategoryId: req.CategoryId,
		BrandsId:   req.BrandId,
	}
	global.DB.Save(&categoryBrand)
	return &proto.CategoryBrandResponse{Id: categoryBrand.ID}, nil
}

/*
*
删除分类和品牌的关系
*/
func (g *GoodsServer) DeleteCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*emptypb.Empty, error) {
	if result := global.DB.Delete(&model.GoodsCategoryBrand{}, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "删除分类品牌关联失败")
	}
	return &emptypb.Empty{}, nil
}

/*
*
更新分类和品牌的关系
*/
func (g *GoodsServer) UpdateCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*emptypb.Empty, error) {
	var categoryBrand model.GoodsCategoryBrand
	var category model.Category
	var brand model.Brands
	if result := global.DB.First(&categoryBrand, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "找不到分类品牌")
	}
	if result := global.DB.First(&category, req.CategoryId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "找不到分类")
	}
	if result := global.DB.First(&brand, req.BrandId); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "找不到品牌")
	}
	categoryBrand = model.GoodsCategoryBrand{
		CategoryId: req.CategoryId,
		BrandsId:   req.BrandId,
	}
	global.DB.Save(&categoryBrand)
	return &emptypb.Empty{}, nil
}
